cmake_minimum_required(VERSION 2.8.12)
project(UBOOT_SAV5XX_Tests)
option(BUILD_TESTS "Build unit tests" ON)

include(Testing)

# Include the Google Test framework
set(GTEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../external/googletest)

if (NOT UNIT_TESTS_REPORT_DIR)
    set(UNIT_TESTS_REPORT_DIR ${CMAKE_BINARY_DIR}/test_reports)
    message(WARNING "UNIT_TESTS_REPORT_DIR not set, Applying default: ${UNIT_TESTS_REPORT_DIR}")
endif()

# Set up include directories similar to your build script
include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/..  # Include the U-Boot SAV5XX source directory
)

# Add Google Test as a subdirectory
add_subdirectory(${GTEST_DIR} ${CMAKE_BINARY_DIR}/googletest)

# Create the test executable
add_executable(test_sigmastar_sav5xx_uboot_example
    test_example.cpp
)

# Define compile flags similar to your build script
target_compile_definitions(test_sigmastar_sav5xx_uboot_example PRIVATE
    HOST_UNIT_TESTS
    USE_HOSTCC
    CONFIG_CMD_MTDPARTS
)

# Add compiler flags
target_compile_options(test_sigmastar_sav5xx_uboot_example PRIVATE
    -g  # Debug info
)

# Link against Google Test and any U-Boot libraries
target_link_libraries(test_sigmastar_sav5xx_uboot_example PRIVATE
    gtest
    gtest_main
    gmock
    gmock_main
)

# Add the test to CTest
add_test(NAME UBootSAV5xxTests COMMAND test_sigmastar_sav5xx_uboot_example --gtest_output=json:${UNIT_TESTS_REPORT_DIR}/)
install(TARGETS test_sigmastar_sav5xx_uboot_example DESTINATION ${CMAKE_INSTALL_FULL_BINDIR})
